package com.demo.common.lock.config;

import com.demo.common.core.utils.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        SingleServerConfig configRet = config.useSingleServer().setAddress("redis://" + redisHost + ":" + redisPort);
        if (StringUtils.isNotEmpty(password)) {
            configRet.setPassword(password);
        }
        return Redisson.create(config);
    }
}
